<?php
class updateoptModel extends model
{

/**
     * Get 得到sites
     * @access public
     * @return array
     */
    public function get_sites_info($pager = null,$where_to=null)
    {
    	$where="1=1";
        if(trim($where_to->key_word)!=''){
    		$where.=" and (sites_name like '%".$where_to->key_word."%' or sites_remark like '%".$where_to->key_word."%')";
    	}
        if($where_to->status!='all'){
    		$where.=" and status=".$where_to->status;
    	}
        return $this->dao->select('*')->from('sites_info')->where($where)->orderBy('sites_id desc')->page($pager)->fetchAll();
    }
 /**
     * 根据id得到sites
     * @param unknown_type $unit_id
     */
    public function get_sites_info_by_id($sites_id){
    	$data=$this->dao->select('*')->from('sites_info')->where("sites_id='$sites_id'")->fetch();
        return $data;
    }
 /**
     * 创建sites信息表
     */
    public function create_sites_info(){
    	$sites_info = fixer::input('post')->specialchars('sites_name,type_id,area_id,area_id2,unit_id,main_domain,visit_domain,visit_type,pre_v6,sites_remark,status')->get();
    	$data=new stdClass();
    	$data->sites_name=$sites_info->sites_name;
    	$data->type_id=$sites_info->type_id;
    	$data->area_id=$sites_info->area_id;
    	$data->area_id2=$sites_info->area_id2;
    	$data->unit_id=$sites_info->unit_id;
    	$data->main_domain=$sites_info->main_domain;
    	$data->visit_domain=$sites_info->visit_domain;
    	$data->visit_type=$sites_info->visit_type;
    	$data->pre_v6=$sites_info->pre_v6;
    	$data->sites_remark=$sites_info->sites_remark;
    	$data->status=$sites_info->status;
        $this->dao->insert('sites_info')->data($data)->exec();
        return $this->dao->lastInsertID();
    }
    /**
     * 更新sites
     */
    public function update_sites_info($data,$sites_id){
        $this->dao->update('sites_info')->data($data)->where('sites_id')->eq($sites_id)->exec();
    }
    
   /**
     * 删除sites
     * @param $id
     */
    public function delete_sites_info($sites_id)
    {
        $this->dao->delete()->from('sites_info')->where('sites_id')->eq($sites_id)->exec();
    }
/***********************************************************************************************/
/**
     * 得到基本配置信息
     * @param unknown_type $unit_id
     */
    public function get_basicconf_info(){
    	$data=$this->dao->select('*')->from('basicconf_info')->where("id=1")->fetch();
        return $data;
    }
/**
     * 更新单位信息表
     */
    public function update_basicconf_info($data){
        $this->dao->update('basicconf_info')->data($data)->where('id')->eq(1)->exec();
    }
    /*************************************************************************************/
/**
     * Get 得到单位管理信息列表
     * 
     * @access public
     * @return array
     */
    public function get_unit_info($pager = null,$where_to=null)
    {
    	$where="1=1";
        if(trim($where_to->key_word)!=''){
    		$where.=" and (unit_name like '%".$where_to->key_word."%' or unit_remark like '%".$where_to->key_word."%' or unit_linkman like '%".$where_to->key_word."%' or unit_contact like '%".$where_to->key_word."%')";
    	}
    	//时间判断
        if(trim($where_to->datetime_start)!=''){
    		$where.=" and unit_createtime >".strtotime(trim($where_to->datetime_start));
    	}
        if(trim($where_to->datetime_end)!=''){
    		$where.=" and unit_createtime <".strtotime(trim($where_to->datetime_end));
    	}
        return $this->dao->select('*')->from('unit_info')->where($where)->orderBy('unit_id desc')->page($pager)->fetchAll();
    }
 /**
     * 根据id得到单位信息
     * @param unknown_type $unit_id
     */
    public function get_unit_info_by_id($unit_id){
    	$data=$this->dao->select('*')->from('unit_info')->where("unit_id='$unit_id'")->fetch();
        return $data;
    }
 /**
     * 创建单位信息表
     */
    public function create_unit_info(){
    	$unit_info = fixer::input('post')->specialchars('unit_name,unit_remark,unit_linkman,unit_contact')->get();
    	$data=new stdClass();
    	$data->unit_name=$unit_info->unit_name;
    	$data->unit_remark=$unit_info->unit_remark;
    	$data->unit_linkman=$unit_info->unit_linkman;
    	$data->unit_contact=$unit_info->unit_contact;
    	$data->unit_createtime=time();
        $this->dao->insert('unit_info')->data($data)->exec();
        return $this->dao->lastInsertID();
    }
    /**
     * 更新单位信息表
     */
    public function update_unit_info($data,$unit_id){
        $this->dao->update('unit_info')->data($data)->where('unit_id')->eq($unit_id)->exec();
    }
    
   /**
     * 删除单位信息
     * @param $id
     */
    public function delete_unit_info($unit_id)
    {
        $this->dao->delete()->from('unit_info')->where('unit_id')->eq($unit_id)->exec();
    }
    /****************************************************************************************************/
/**
     * Get 得到类型管理信息列表
     * 
     * @access public
     * @return array
     */
    public function get_type_info($pager = null,$where_to=null)
    {
    	$where="1=1";
        if(trim($where_to->key_word)!=''){
    		$where.=" and (type_name like '%".$where_to->key_word."%' or type_remark like '%".$where_to->key_word."%')";
    	}
        return $this->dao->select('*')->from('type_info')->where($where)->orderBy('type_id desc')->page($pager)->fetchAll();
    }
 /**
     * 根据id得到网站类型信息
     * @param unknown_type $unit_id
     */
    public function get_type_info_by_id($type_id){
    	$data=$this->dao->select('*')->from('type_info')->where("type_id='$type_id'")->fetch();
        return $data;
    }
 /**
     * 创建类型信息表
     */
    public function create_type_info(){
    	$type_info = fixer::input('post')->specialchars('type_name,type_remark')->get();
    	$data=new stdClass();
    	$data->type_name=$type_info->type_name;
    	$data->type_remark=$type_info->type_remark;
        $this->dao->insert('type_info')->data($data)->exec();
        return $this->dao->lastInsertID();
    }
    /**
     * 更新网站类型信息表
     */
    public function update_type_info($data,$type_id){
        $this->dao->update('type_info')->data($data)->where('type_id')->eq($type_id)->exec();
    }
    
   /**
     * 删除网站类型信息
     * @param $id
     */
    public function delete_type_info($type_id)
    {
        $this->dao->delete()->from('type_info')->where('type_id')->eq($type_id)->exec();
    }
    
   /****************************************************************************************************/
/**
     * Get 得到area
     * 
     * @access public
     * @return array
     */
    public function get_area_info($pager = null,$where_to=null)
    {
    	$where="1=1";
        if(trim($where_to->key_word)!=''){
    		$where.=" and (name like '%".$where_to->key_word."%')";
    	}
    	if(trim($where_to->reid)!=''){
    		$where.=" and reid=$where_to->reid";
    	}
        return $this->dao->select('*')->from('area_info')->where($where)->orderBy('id asc')->page($pager)->fetchAll();
    }
 /**
     * 根据id得到area
     * @param unknown_type $unit_id
     */
    public function get_area_info_by_id($area_id){
    	$data=$this->dao->select('*')->from('area_info')->where("id='$area_id'")->fetch();
        return $data;
    }
 /**
     * 创建area信息表
     */
    public function create_area_info($data){
        $this->dao->insert('area_info')->data($data)->exec();
        return $this->dao->lastInsertID();
    }
    /**
     * 更新area
     */
    public function update_area_info($data,$area_id){
        $this->dao->update('area_info')->data($data)->where('id')->eq($area_id)->exec();
    }
    
   /**
     * 删除area
     * @param $id
     */
    public function delete_area_info($area_id)
    {
        $this->dao->delete()->from('area_info')->where('id')->eq($area_id)->exec();
    }
    /******************************************************************************************/
    //站点检测函数应用
    
    public function test_url($url,$type='ipv6'){
	   
   		$curl = curl_init ( $url );
		curl_setopt ( $curl, CURLOPT_NOBODY, true );
		curl_setopt ( $curl, CURLOPT_TIMEOUT, 5 );
		$result = curl_exec ( $curl );
		$found = false;
		if ($result !== false) {
			$statusCode = curl_getinfo ( $curl, CURLINFO_HTTP_CODE );
			//echo $statusCode;
			if ($statusCode == 200||$statusCode == 302) {
				$found = true;
			}
		}
		curl_close ( $curl );
		$data['ipv4_status']=0;
		$data['ipv6_status']=0;
		$data['web']=$url;
		$data['ipv6']="";
		$data['ipv4']="";
		if($found==true){
			$data['ipv4_status']=1;
			if($type=='ipv6'){//只有检测ipv6时才进行v6检测
				$data['ipv6']=$this->gethostbyname6($url);
				if($data['ipv6']!=false)
				{
					$data['ipv6_status']=1;
				}
			}
			
		}
		return $data;
    }
    
	//升级检测相关
	public function gethostbyname6($host, $try_a = false) {
		// get AAAA record for $host
		// if $try_a is true, if AAAA fails, it tries for A
		// the first match found is returned
		// otherwise returns false
		$dns = $this->gethostbynamel6 ( $host, $try_a );
		if ($dns == false) {
			return false;
		} else {
			return $dns [0];
		}
	}
	
	public function gethostbynamel6($host, $try_a = false) {
		// get AAAA records for $host,
		// if $try_a is true, if AAAA fails, it tries for A
		// results are returned in an array of ips found matching type
		// otherwise returns false
		
		$dns6 = dns_get_record ( $host, DNS_AAAA );		
		if ($try_a == true) {
			$dns4 = dns_get_record ( $host, DNS_A );
			$dns = array_merge ( $dns4, $dns6 );
		} else {
			$dns = $dns6;
		}
		$ip6 = array ();
		$ip4 = array ();
		foreach ( $dns as $record ) {
			if ($record ["type"] == "A") {
				$ip4 [] = $record ["ip"];
			}
			if ($record ["type"] == "AAAA") {
				$ip6 [] = $record ["ipv6"];
			}
		}
		if (count ( $ip6 ) < 1) {
			if ($try_a == true) {
				if (count ( $ip4 ) < 1) {
					return false;
				} else {
					return $ip4;
				}
			} else {
				return false;
			}
		} else {
			return $ip6;
		}
	}
}
